<template>
  <el-dialog
    class="save-or-update-device"
    :visible="visible"
    :close-on-click-modal="false"
    :before-close="handleClose"
    :title="title"
    width="574px"
  >
    <el-form ref="form" :model="formData" label-width="100px" label-suffix="：" :rules="rules">
      <el-form-item v-if="value.id" prop="name" label="设备名">
        <el-input v-model="formData.name" placeholder="请输入设备名" disabled clearable />
      </el-form-item>
      <el-form-item prop="remarkName" label="备注名称">
        <el-input v-model="formData.remarkName" placeholder="请输入备注名" clearable />
      </el-form-item>
      <el-form-item prop="productId" label="所属产品">
        <SelectProduct
          :value="value.productVo"
          style="width: 100%;"
          @change="({id}={id:''})=>formData.productId=id"
        />
      </el-form-item>
      <template v-if="value&&value.id">
        <el-form-item prop="netType" label="入网类型">
          {{ value.productVo && value.productVo.netTypeName }}
        </el-form-item>
        <el-form-item prop="deviceSourceEnum" label="数据来源">
          {{ value.source || '外部系统' }}
        </el-form-item>
        <el-form-item prop="" label="设置分组">
          {{ value.group || '--' }}
        </el-form-item>
      </template>
      <el-form-item prop="enableStateEnum" label="启用状态">
        <SwitchDeviceState v-model="formData.enableStateEnum" />
      </el-form-item>
      <el-form-item v-if="value&&value.id" prop="onlineStateEnum" label="在线状态">
        {{ value.online ? '在线' : '离线' }}
      </el-form-item>
      <el-form-item label="ue4Code" prop="ue4Code">
        <el-input v-model="formData.ue4Code"></el-input>
      </el-form-item>
      <el-form-item v-if="value&&value.id" prop="regionId" label="绑定区域">
        <el-select v-model="formData.regionId" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="item in areaList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="remark" label="备注说明">
        <el-input v-model="formData.remark" type="textarea" clearable />
      </el-form-item>
    </el-form>
    <span slot="footer" class="justify-center">
      <el-button v-loading="loading" type="primary" @click="handleSubmit">提交</el-button>
      <el-button @click="handleClose">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { apiAddDevice, apiUpdateDevice } from '@/views/device/deploy-device/api'
import SelectProduct from '@/views/device/create-product/components/select-product'
import SwitchDeviceState from './switch-device-state'
import { apiGetAllArea } from '@/views/monitor/global/api'

export default {
  name: 'SaveOrUpdateDevice',
  components: { SelectProduct, SwitchDeviceState },
  props: {
    title: { type: String, required: true, default: '新建设备' },
    visible: { type: Boolean, required: true, default: false },
    value: {
      type: Object,
      default: () => {
        return { id: 0 }
      },
    },
  },
  data() {
    return {
      loading: false,
      formData: {
        name: '',
        remarkName: '',
        remark: '',
        productId: '',
        onlineState: '',
        parentId: '',
        enableStateEnum: 'DISABLE',
      },
      areaList: [],
    }
  },
  computed: {
    rules() {
      return {
        name: { required: true, message: '设备名不能为空', trigger: 'blur' },
        remarkName: { required: true, message: '备注名称不能为空', trigger: 'blur' },
        productId: { required: true, message: '所属产品不能为空', trigger: 'change' },
      }
    },
  },
  watch: {
    value(value) {
      if (value) {
        this.formData = value
      }
    },
  },

  mounted() {
    this.handleGetAllAreaList()
  },
  methods: {
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const formData = {}
          for (const key in this.formData) {
            if (this.formData[key] && this.formData[key].length) {
              formData[key] = this.formData[key]
            }
          }
          if (this.value.id) {
            this.updateDevice(formData)
          } else {
            this.addDevice(formData)
          }
        }
      })
    },
    async addDevice(formData) {
      this.loading = true
      try {
        const device = await apiAddDevice(formData)
        this.$message.success('新建成功')
        this.$emit('change', device)
        this.handleClose()
      } catch (e) {
        this.$message.error(e)
      }
      this.loading = false
    },
    async updateDevice(device) {
      this.loading = true
      try {
        await apiUpdateDevice(device)
        this.$message.success('修改成功')
        this.$emit('change', device)
        this.handleClose()
      } catch (e) {
        this.$message.error(e)
      }
    },
    handleClose() {
      if (this.value.id) {
        this.formData = this.value
      }
      this.loading = false
      this.$refs.form.clearValidate()
      this.$emit('update:visible', false)
    },

    async handleGetAllAreaList() {
      try {
        const res = await apiGetAllArea()
        this.areaList = res
      } catch (error) {
        this.$message.error(error)
      }
    },
  },
}
</script>

<style scoped lang="scss">
.save-or-update-device {
  ::v-deep .el-dialog__body {
    padding: 10px 20px;
  }

  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
}
</style>
